package boxe.timer.view;

/**
 * mode view to customize the mode
 * 
 * @author Julien Perriard
 * @date 18.05.2010
 *
 */

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

import boxe.timer.Mode;
import boxe.timer.env.Propertie;
import boxe.timer.manager.ModeManager;
import boxe.timer.manager.RunManager;

public class ModeView extends JFrame implements ActionListener{
	private JLabel lblName, lblRound;
	private JTextField txtName, txtRound;
	private JButton cmdSave, cmdCancel;
	private Box[] boxes;
	private JPanel cmdSouth, panelBorder, gridPanel;
	private JComboBox combo;
	private GridBagConstraints constraints;
	private ModeManager modeManager;
	
	public ModeView(ModeManager modeManager) {
		this.modeManager = modeManager;
		panelBorder = new JPanel(new BorderLayout());	
		gridPanel = new JPanel(new GridBagLayout());
		String [] tab = new Propertie().getRoundArray();				
		combo = new JComboBox(tab);
		combo.setSelectedIndex(1);
		combo.setPreferredSize(new Dimension(70, 25));
		combo.setMaximumSize(new Dimension(70, 25));
		lblName = new JLabel("Mode name");
		lblRound = new JLabel("number of round");
		txtName = new JTextField();
		txtName.setPreferredSize(new Dimension(150, 25));
		txtName.setMaximumSize(new Dimension(150, 25));
		txtRound = new JTextField(2);
		cmdSave = new JButton("Save");
		cmdCancel = new JButton("Cancel");
		cmdSouth = new JPanel();
		cmdSouth.add(cmdCancel);
		cmdSouth.add(cmdSave);

		//add to left Panel
		constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.BOTH;
		constraints.anchor= GridBagConstraints.WEST;
		constraints.gridx=0;
		constraints.gridy=0;
		constraints.insets = new Insets(8,8,0,0);
		gridPanel.add(lblName,constraints);		
		constraints.gridx=1;
		constraints.gridy=0;
		gridPanel.add(txtName,constraints);	
		constraints.gridx=0;
		constraints.gridy=1;
		gridPanel.add(lblRound,constraints);	
		constraints.gridx=1;
		constraints.gridy=1;
		gridPanel.add(combo,constraints);	

		JPanel panel = new JPanel();
		TitledBorder title;
		title = BorderFactory.createTitledBorder("Mode");
		gridPanel.setBorder(title);

		panelBorder.add(gridPanel, BorderLayout.CENTER);
		//panelBorder.add(boxes[1], BorderLayout.EAST);
		panelBorder.add(cmdSouth, BorderLayout.SOUTH);
		panelBorder.setPreferredSize(new Dimension(300,170));
		cmdSave.setActionCommand(Propertie.SAVE);
		cmdSave.addActionListener(this);
		cmdCancel.setActionCommand(Propertie.CANCEL);
		cmdCancel.addActionListener(this);
	
		this.setSize(300,170);
		this.setTitle("Mode view");
//		centrage
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = this.getSize();
		this.setLocation(
			(screenSize.width - frameSize.width)/2,
			(screenSize.height-frameSize.height)/2
		);
		this.add(panelBorder);
		//look and feel
		try {
			UIManager.setLookAndFeel(
					UIManager.getSystemLookAndFeelClassName());
			SwingUtilities.updateComponentTreeUI(this);
		}catch (Exception e) {
			e.printStackTrace();
		}
		//this.pack();
		//frame.setIconImage(new ImageIcon(ICON).getImage());
		setResizable(false);
	}


	public void actionPerformed(ActionEvent e) {
		String item = String.valueOf(((AbstractButton) e.getSource())
				.getActionCommand());
		
		Mode mode = modeManager.getCurrentMode();
		
		if(item.equals(Propertie.SAVE)){
			mode.setName(txtName.getText());
			mode.setNbRound(combo.getSelectedIndex());
			if(mode != modeManager.getNewMode()) {
				modeManager.addMode(mode);
				modeManager.setCurrentMode(mode);
				//modeManager.setDefaultRunPack();
				modeManager.refreshListMode();
			}
						
			this.setVisible(false);
			modeManager.setSelectedIndex();
		}

		else if(item.equals(Propertie.CANCEL)) {
			this.setVisible(false);
			
		}
	}
	
	public void setDefaultValue(Mode mode) {
		txtName.setText(mode.getName());
		combo.setSelectedIndex(mode.getNbRound());
	}
}
